package com.yanchangapp.ui.activity._n_manage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.luck.picture.lib.tools.ScreenUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yanchangapp.R;
import com.yanchangapp.adapter._n_manage.GroupLookLinkManAdapter;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.model.bean.EmployeeBean;
import com.yanchangapp.model.bean.LookLinkManBean;
import com.yanchangapp.model.bean.LookLinkWorkBean;
import com.yanchangapp.model.bean.PlanWorkBean;
import com.yanchangapp.model.cache.UserCache;
import com.yanchangapp.model.request.MakePlanManRequest;
import com.yanchangapp.model.request.MakePlanWorkRequest;
import com.yanchangapp.ui.presenter.LookLinkManAtPresenter;
import com.yanchangapp.ui.view.LookLinkManAtView;
import com.yanchangapp.widget.SetDieTimeDialog;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.YCLoadingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * page: 查看关联员工
 * <p>
 * 勾选之后分配工作
 */
public class LookLinkManActivity extends BaseActivity<LookLinkManAtView, LookLinkManAtPresenter> implements LookLinkManAtView {

    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.rvLinkWork)
    RecyclerView rvLinkWork;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tvDes)
    TextView tvDes;
    @BindView(R.id.llEmptyMsg)
    LinearLayout llEmptyMsg;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;
    @BindView(R.id.btnMakePlan)
    Button btnMakePlan;
    @BindView(R.id.bottom)
    LinearLayout bottom;

    private GroupLookLinkManAdapter mGroupLookLinkManAdapter;
    private List<LookLinkManBean> mLookLinkManBeans = new ArrayList<>();
    private long itemID;

    private void setLoadingState(LoadingType loadingType) {
        switch (loadingType) {
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE) {
                    ycLoading.setVisibility(View.VISIBLE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE) {
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE) {
                    refreshLayout.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                }
                break;
            case EMPTY:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.GONE) {
                    llEmptyMsg.setVisibility(View.VISIBLE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE) {
                    refreshLayout.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE) {
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.GONE) {
                    refreshLayout.setVisibility(View.VISIBLE);
                    bottom.setVisibility(View.VISIBLE);
                }
                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE) {
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.GONE) {
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE) {
                    refreshLayout.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                }
                break;
        }
    }

    public static void show(Activity activity, long id) {
        Intent intent = new Intent(activity, LookLinkManActivity.class);
        intent.putExtra("id", id);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
        itemID = getIntent().getLongExtra("id", -1);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_look_link_man;
    }

    @Override
    protected LookLinkManAtPresenter createPresenter() {
        return new LookLinkManAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("查看关联员工");
        initAdapter();
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
        tvDes.setText("暂无关联员工");
    }

    private void initAdapter() {
        rvLinkWork.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rvLinkWork.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.HORIZONTAL, ScreenUtils.dip2px(this, 0.5f), ContextCompat.getColor(this, R.color.borderColorBase)));
        mGroupLookLinkManAdapter = new GroupLookLinkManAdapter(mLookLinkManBeans);
        rvLinkWork.setAdapter(mGroupLookLinkManAdapter);
    }

    @Override
    protected void initData() {
        setLoadingState(LoadingType.LOADING);
        mPresenter.queryLinkManLists(itemID);
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());

        btnMakePlan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkWhichSelected();
            }
        });

        mGroupLookLinkManAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.checkbox){
                boolean selected = mLookLinkManBeans.get(position).isSelected();
                mLookLinkManBeans.get(position).setSelected(!selected);
            }else if (view.getId() == R.id.llTime){
                Integer time =  mLookLinkManBeans.get(position).getPredictTime();
                showSetDieTimeDialog(position,time);
            }else if (view.getId() == R.id.llItem){
                Boolean check = mLookLinkManBeans.get(position).getCheck();
                if (check == null || !check){
                    boolean selected = mLookLinkManBeans.get(position).isSelected();
                    mLookLinkManBeans.get(position).setSelected(!selected);
                    mGroupLookLinkManAdapter.notifyItemChanged(position);
                }
            }
        });
    }

    private void checkWhichSelected() {
        MakePlanWorkRequest makePlanWorkRequest = new MakePlanWorkRequest();
        makePlanWorkRequest.setWorkItemId(itemID);
        List<PlanWorkBean> list = new ArrayList<>();
        for (int i=0;i<mLookLinkManBeans.size();i++){
            LookLinkManBean workBean = mLookLinkManBeans.get(i);
            if (workBean.isSelected()){
                list.add(new PlanWorkBean(workBean.getId(),
                        workBean.getPredictTime() == null ? 1:workBean.getPredictTime(),
                        workBean.getPredictTime() == null?2:workBean.getPredictTime()));
            }
        }
        if (list.size() == 0){
            UIUtils.showToast("请选择工作项");
            return;
        }
        makePlanWorkRequest.setWorkItemList(list);
        mPresenter.actionMakePlan(makePlanWorkRequest);
    }

    private void showSetDieTimeDialog(int position, Integer time) {
        new SetDieTimeDialog.Builder(this)
                .setPrice(time)
                .setEnsureListener(dieTime -> {
                    mLookLinkManBeans.get(position).setPredictTime(dieTime);
                    mGroupLookLinkManAdapter.notifyItemChanged(position);
                }).build().show();
    }

    @Override
    public void queryLookLinkManSuccess(List<LookLinkManBean> lists) {
        if (lists.size() == 0) {
            setLoadingState(LoadingType.EMPTY);
        } else {
            setLoadingState(LoadingType.SUCCESS);
            mLookLinkManBeans.clear();
            mLookLinkManBeans.addAll(lists);
            mGroupLookLinkManAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void queryLookLinkManFailure(String err) {
        setLoadingState(LoadingType.ERROR);
        UIUtils.showToast(err);
    }

    @Override
    public void makePlanSuccess(String success) {
        UIUtils.showToast(success);
        finish();
    }

    @Override
    public void makePlanFailure(String err) {
        UIUtils.showToast(err);
    }

}
